
angular.module("directives", [])
	.directive("myPhoneValidator", function () {
      return {
    	  restrict: 'A',
    	  require: 'ngModel',
    	  link: function (scope, elem, attrs, ctrl) {
    		  var val = elem.val();
    		  var phonePattern = /^0[0-9]{9}$/;
    		  
    		  var regex = new RegExp(phonePattern);
    		  
    		  // add a parser that will process each time the value is 
              // parsed into the model when the user updates it.
              ctrl.$parsers.unshift(function(value) {
            	  var valid = true;
            	  
                  if(typeof value != 'undefined' && value != ''){
                	  // test and set the validity after update.
                	  valid = regex.test(value);
                	  ctrl.$setValidity('regexValidate', valid);
                  }else{
                	  ctrl.$setValidity('regexValidate', true);
                  }
                  // if it's valid, return the value to the model, 
                  // otherwise return undefined.
                  
            	  return valid ? value : undefined;
              });
              
              // add a formatter that will process each time the value 
              // is updated on the DOM element.
              ctrl.$formatters.unshift(function(value) {
            	  
            	  if(typeof value != 'undefined' && value != ''){
            		  // validate.
//            		  ctrl.$setValidity('regexValidate', regex.test(value));
            		  ctrl.$setValidity('regexValidate', true);
            	  }
                  
                  // return the value or nothing will be written to the DOM.
                  return value;
              });
    	  },
      };
   })

   .directive("myCodePostalValidator", function () {
    return {
      restrict: 'A',
      require: 'ngModel',
      link: function (scope, elem, attrs, ctrl) {
        var val = elem.val();
        var phonePattern = /^[0-9]{5}$/;
        
        var regex = new RegExp(phonePattern);
        
        // add a parser that will process each time the value is 
        // parsed into the model when the user updates it.
        ctrl.$parsers.unshift(function(value) {
          // test and set the validity after update.

          ctrl.$setValidity('codepostalValidate', regex.test(value));
          
          // if it's valid, return the value to the model, 
          // otherwise return undefined.
          // return valid ? value : undefined;
          return value;
        });
        
        // add a formatter that will process each time the value 
        // is updated on the DOM element.
        ctrl.$formatters.unshift(function(value) {
          // validate.
          if(typeof value != 'undefined' && value != '') {

            ctrl.$setValidity('codepostalValidate', regex.test(value));
          }
          
          // return the value or nothing will be written to the DOM.
          return value;
        });
      },
    };
  })


.directive("urlvalidator", function () {
	return {
	  restrict: 'A',
	  require: 'ngModel',
	  link: function (scope, elem, attrs, ctrl) {
		  var val = elem.val();
      
		  // var phonePattern = /^[0-9]{5}$/;
		  var urlPattern = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;

		  var regex = new RegExp(urlPattern);
		  
		  ctrl.$parsers.unshift(function(value) {
        
        value = $.trim(value);

        if(typeof value != 'undefined' && value != '') {
		      ctrl.$setValidity('urlvalidator', regex.test(value));
        } else {
          ctrl.$setValidity('urlvalidator', true);
        }
			  
			  return value;
		  });
		  
		  ctrl.$formatters.unshift(function(value) {
        
        value = $.trim(value);

			  // validate.
        if(typeof value != 'undefined' && value != '') {

          ctrl.$setValidity('urlvalidator', regex.test(value));
        } else {
          ctrl.$setValidity('urlvalidator', true);
        }

			  return value;
		  });
	  },
	};
})
